data <- read.csv("USA_GDP_Annual.csv")
Note: All values are measured in millions of USD.
# convert to string
#data$Year <- as.character(data$Year)
# remove commas & convert to numeric
data$GDP <- as.numeric(gsub("," ,"" , data$GDP))
data$Consumption <- as.numeric(gsub("," ,"" , data$Consumption))
data$Investment <- as.numeric(gsub("," ,"" , data$Investment))
data$Net.Exports <- as.numeric(gsub("," ,"" , data$Net.Exports))
data$Government.Spending <- as.numeric(gsub("," ,"" , data$Government.Spending))
# keep years that are multiples of 10
data10 <- subset(data, Year %% 10 == 0)
#data10$Year <- as.character(data10$Year)
# add proportion of consumption, investment, net exports, and government spending
data10 <- data10 %>% mutate(
Consumption.prop = Consumption / GDP,
Investment.prop = Investment / GDP,
Net.Exports.prop = Net.Exports / GDP,
Government.Spending.prop = Government.Spending / GDP
)
# each proportion needs to have its own row
data %>%
plot_ly() %>%
add_lines(x = ~Year, y = ~GDP) %>%
layout(title = "GDP by Year")
data10 %>%
plot_ly() %>%
add_bars(x = ~Year, y = ~GDP, color = ~Consumption.prop) %>%
layout(barmode = "stack", title = "GDP by Year")
## Warning: textfont.color doesn't (yet) support data arrays
## Warning: textfont.color doesn't (yet) support data arrays